package com.erpanderp.wam.tm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;
import com.erpanderp.wam.tm.model.entity.RecruitmentVendor;
import com.erpanderp.wam.tm.model.service.VendorRegistrationDAO;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class VendorRegistrationsBean implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private List<RecruitmentVendor> recruitmentVendors = new ArrayList<RecruitmentVendor>();
	private VendorRegistrationDAO vendorRegistrationDAO=new VendorRegistrationDAO();
	private RecruitmentVendor selectedVendor = new RecruitmentVendor();
	private DataTable vendorTable;
	private boolean FORM_VALID;
	private int sno=1;

	public VendorRegistrationsBean() {
		
		recruitmentVendors = vendorRegistrationDAO.getRecruitmentVendors();
		for(RecruitmentVendor recruitmentVendor : recruitmentVendors){
			recruitmentVendor.setsNo(sno);
			sno++;
		}
		
	}

	public void updateRecruitmentVendor(ActionEvent e){
		 System.out.println("INSIDE UPDATE METHOD:::::");
			
		 validateForm();
			
			if(FORM_VALID){
				System.out.println("Updating Employee...");
			//	selectedVendor.setCreateDate(new Date());
				selectedVendor.setDateModified(new Date());
				vendorRegistrationDAO.updateRecruitmentVendor(selectedVendor);	
				
				RequestContext.getCurrentInstance().execute("vendorEditDialog.hide();");
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  " Vendor updated successfully.", ""));
			}else{
				RequestContext.getCurrentInstance().execute("vendorEditDialog.show();");			
			}
	}
	
	//Confirmation action 
	public void confirmVendorRequest(ActionEvent e){			
		validateForm();
		if(FORM_VALID){
			
			if(e.getComponent().getId().equalsIgnoreCase("updateButton")){
				System.out.println("=================INSIDE UPDATE============");
				RequestContext.getCurrentInstance().execute("confirmationUpdate.show();");
			}
			if(e.getComponent().getId().equalsIgnoreCase("deleteButton")){
				System.out.println("=================INSIDE UPDATE============");
				RequestContext.getCurrentInstance().execute("confirmationDelete.show();");
			}
			//RequestContext.getCurrentInstance().execute("vendorEditDialog.show();");
			 
		}
	}
	
	public void viewOrEditActionListener(ActionEvent e){
		System.out.println("Listener...");
		selectedVendor = (RecruitmentVendor) e.getComponent().getAttributes().get("selectedVendor");
	}
	
	public String deleteRecruitmentVendor(){
		 System.out.println("INSIDE DELETE METHOD:::::");
			
		 selectedVendor.setStatus("INACTIVE");
		 selectedVendor.setDateModified(new Date());
		 vendorRegistrationDAO.updateRecruitmentVendor(selectedVendor);	
		 RequestContext.getCurrentInstance().execute("vendorEditDialog.hide();");
		 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  " Vendor deleted successfully.", ""));
		
		 return "vendorRegistrations";
	}
	
	private void validateForm(){
		FORM_VALID = true;					
	  
	   if(ValidationUtil.isEmpty(selectedVendor.getVendorName(), "Vendor Name ", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
	   if(ValidationUtil.isEmpty(selectedVendor.getVendorEmail(), "Vendor E-Mail", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}else {
			System.out.println("EMAIL ENTERED");
			if (ValidationUtil.isEmailValid(selectedVendor.getVendorEmail(), "Vendor E-Mail", null,
					FacesContext.getCurrentInstance()) != null) {
				FORM_VALID = false;
			} 
		}
	   if(ValidationUtil.isEmpty(selectedVendor.getVendorContactNumber(), "Vendor Contact Number", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
	   if(ValidationUtil.isEmpty(selectedVendor.getVendorContactPerson(), "Vendor Contact Person", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
	   if(ValidationUtil.isEmpty(selectedVendor.getVendorAddress(), "Vendor Address", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}		  
	 
		System.out.println("Finally FORM_VALID is :::::"+FORM_VALID);		
	}
	//SETTERS AND GETTERS
	public DataTable getVendorTable() {
		return vendorTable;
	}

	public void setVendorTable(DataTable vendorTable) {
		this.vendorTable = vendorTable;
	}

	public VendorRegistrationDAO getVendorRegistrationDAO() {
		return vendorRegistrationDAO;
	}

	public void setVendorRegistrationDAO(VendorRegistrationDAO vendorRegistrationDAO) {
		this.vendorRegistrationDAO = vendorRegistrationDAO;
	}

	public List<RecruitmentVendor> getRecruitmentVendors() {
		return recruitmentVendors;
	}

	public void setRecruitmentVendors(List<RecruitmentVendor> recruitmentVendors) {
		this.recruitmentVendors = recruitmentVendors;
	}
	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public RecruitmentVendor getSelectedVendor() {
		return selectedVendor;
	}

	public void setSelectedVendor(RecruitmentVendor selectedVendor) {
		this.selectedVendor = selectedVendor;
	}

	public int getSno() {
		return sno;
	}

	public void setSno(int sno) {
		this.sno = sno;
	}

}
